package ipebg.mzk.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by LT on 2016/3/10.
 */
public class PinYinUtil {
	public static String hanZi2FirstChar(String src) {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		String[] pinyinArray = null;
		String result = "";
		try {
			for (int i = 0; i < src.length(); i++) {
				char s = src.charAt(i);
				if ((s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || (s >=48 && s<= 57)) {
					result = result + s;
				} else {
					pinyinArray = PinyinHelper.toHanyuPinyinStringArray(src.charAt(i), outputFormat);
					if (pinyinArray != null && pinyinArray.length > 0) {
						result += pinyinArray[0].charAt(0);
					}
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
			badHanyuPinyinOutputFormatCombination.printStackTrace();
		}
		return result;
	}
}
